package org.dromara.system.service.impl;

import jakarta.annotation.Resource;
import org.dromara.system.domain.vo.StoreOrderMonthVo;
import org.dromara.system.mapper.TxTuanGouMapper;
import org.dromara.system.mapper.WbyTuanGouMapper;
import org.dromara.system.service.TuanGouService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 15063
 * @title: TuanGouServiceImpl
 * @projectName zhsq-daping-master
 * @description: TODO
 * @date 2025/9/1516:37
 */
@Service
public class TuanGouServiceImpl  implements TuanGouService {
    @Resource
    private TxTuanGouMapper txTuanGouMapper;
    @Resource
    private WbyTuanGouMapper wbyTuanGouMapper;
    @Override
    public Map<String, Object> queryOrderData() {
        Map<String, Object> resultMap = new HashMap<>();
        //铁西当日订单数
        Integer txTodayOrderNum = txTuanGouMapper.queryTodayOrderNum();
        //铁西获取总订单数
        Integer txTotalOrderNum = txTuanGouMapper.queryTotalOrderNum();
        //铁西订单总金额
        BigDecimal txTotalOrderSumMoney= txTuanGouMapper.queryTotalOrderSumMoney();

        //文博苑当日订单数
        Integer wbyTodayOrderNum = wbyTuanGouMapper.queryTodayOrderNum();
        //文博苑获取总订单数
        Integer wbyTotalOrderNum = wbyTuanGouMapper.queryTotalOrderNum();
        //文博苑订单总金额
        BigDecimal wbyTotalOrderSumMoney= wbyTuanGouMapper.queryTotalOrderSumMoney();
        resultMap.put("todayOrderNum",txTodayOrderNum+wbyTodayOrderNum);
        resultMap.put("totalOrderNum",txTotalOrderNum+wbyTotalOrderNum);
        resultMap.put("totalOrderSumMoney",txTotalOrderSumMoney.add(wbyTotalOrderSumMoney));
        return resultMap;
    }

    @Override
    public Map<String, Object> queryGoodsData() {
        Map<String, Object> resultMap = new HashMap<>();
        //铁西
        Integer txProductNum = txTuanGouMapper.queryGoodsNum();
        Integer txCategoryNum = txTuanGouMapper.queryCountCategory();
        //文博苑

        Integer wbyProductNum = wbyTuanGouMapper.queryGoodsNum();
        Integer wbyCategoryNum = wbyTuanGouMapper.queryCountCategory();

        resultMap.put("productNum",txProductNum+wbyProductNum);
        resultMap.put("categoryNum",txCategoryNum+wbyCategoryNum);
        return resultMap;
    }
    /**
     * 查询热门商品信息   获取销量top10的商品信息
     */
    @Override
    public Map<String, Object> queryTop10GoodsData() {
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> stringObjectMap1 = txTuanGouMapper.queryTop10GoodsData();
        Map<String, Object> stringObjectMap2 = wbyTuanGouMapper.queryTop10GoodsData();
        resultMap.put("txGoodsMap",stringObjectMap1);
        resultMap.put("wbyGoodsMap",stringObjectMap2);
        return resultMap;
    }
    /**
     * .各个分类商品的销售占比，统计两个社区所有的商品分类情况及其占比情况
     */
    @Override
    public Map<String, Object> queryCategoryGoods() {
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> categoryGoods1 = txTuanGouMapper.queryCategoryGoods();
        Map<String, Object> categoryGoods2 = wbyTuanGouMapper.queryCategoryGoods();
        resultMap.put("txGoodsMap",categoryGoods1);
        resultMap.put("wbyGoodsMap",categoryGoods2);
        return resultMap;
    }

    /**
     * .各个分类商品的销售占比，统计两个社区所有的商品分类情况及其占比情况
     */
    @Override
    public Map<String, Object> queryMonthOrderGoods() {
        Map<String, Object> resultMap = new HashMap<>();
        List<StoreOrderMonthVo> monthOrderList = new ArrayList<>();
        List<StoreOrderMonthVo> storeOrderMonthVos1 = txTuanGouMapper.queryMonthOrderGoods();
        List<StoreOrderMonthVo> storeOrderMonthVos2 = wbyTuanGouMapper.queryMonthOrderGoods();
        //获取每个月份的数据，将每个月份的数据放在一个List中
        for (StoreOrderMonthVo storeOrderMonthVo1 : storeOrderMonthVos1) {
            StoreOrderMonthVo monthVo = new StoreOrderMonthVo();
            monthVo.setOrderMonth(storeOrderMonthVo1.getOrderMonth());
            for (StoreOrderMonthVo storeOrderMonthVo2 : storeOrderMonthVos2) {
                if (storeOrderMonthVo1.getOrderMonth().equals(storeOrderMonthVo2.getOrderMonth())) {
                    monthVo.setOrderCount(storeOrderMonthVo1.getOrderCount()+storeOrderMonthVo2.getOrderCount());
                    monthVo.setUniqueCustomers(storeOrderMonthVo1.getUniqueCustomers()+storeOrderMonthVo2.getUniqueCustomers());
                    monthVo.setTotalSales(storeOrderMonthVo1.getTotalSales().add(storeOrderMonthVo2.getTotalSales()));
                }
            }
            monthOrderList.add(storeOrderMonthVo1);
        }
        resultMap.put("monthOrderList",monthOrderList);
        return resultMap;
    }
}
